<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<title>ERP系统 </title>
    <link rel="stylesheet" type="text/css" charset="UTF-8" href="/js/easyui/themes/default/easyui.css" th:href="@{js/easyui/themes/default/easyui.css}"/>
    <link rel="stylesheet" type="text/css" charset="UTF-8" href="/js/easyui/themes/icon.css" th:href="@{js/easyui/themes/icon.css}" />
    <link rel="stylesheet" type="text/css" charset="UTF-8" href="/css/main.css" th:href="@{css/main.css}" />
    <script type="text/javascript" charset="UTF-8" src="/js/easyui/jquery.min.js" th:src="@{js/easyui/jquery.min.js}"></script>
    <script type="text/javascript" charset="UTF-8" src="/js/easyui/jquery.easyui.min.js" th:src="@{js/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" charset="UTF-8" src="/js/easyui/locale/easyui-lang-zh_CN.js" th:src="@{js/easyui/locale/easyui-lang-zh_CN.js}"></script>

</head>
<body>
<div id="deptToolbar">
	<a id="addDept" class="easyui-linkbutton" data-options="plain:true">新增</a> |
	<a id="updateDept" class="easyui-linkbutton" data-options="plain:true">修改</a> |
	<a id="deleteDept" class="easyui-linkbutton" data-options="plain:true">删除</a>
</div>  
<table id ="deptDateGrid"></table>
<div id ="deptDialog"></div>
</body>
	<script type="text/javascript">
        $(function(){
            $('#deptDateGrid').treegrid({
                toolbar: '#deptToolbar',
                title: '组织机构列表',
                iconCls: 'icon icon-org',
                fit : true,//全屏
                border : false,//无边框
                fitColumns : true,//列全屏
                striped : true,//显示斑马线效果
                rownumbers : true,//显示一个行号列
                pagination : true,//分页
                pageSize : 10,//每页显示10条
                pageList : [10,20,30,40,50],//每页条数可选
                pageNumber : 1,//默认显示第1页
                idField:'id',
                treeField: 'FullName',
                url:'/depts',
                method : 'GET',
                frozenColumns: [[
                    { title:'组织机构名称',field: 'name', width: 200 },
                ]],
                columns: [[
                    { title: '备注', field: 'description', width: 300 },
                    { title: 'ParentId', field: 'parentId', hidden: true },
                    { title: '创建时间', field: 'gmtCreate', hidden: true },
                    { title: '修改时间', field: 'gmtModified', hidden: true },
                ]]

            });

            $('#deptDialog').dialog({
                width : 350,
                modal : true,
                closed : true,
                //iconCls : 'icon-add',
                buttons : [{
                    text : '提交',
                    iconCls : 'icon-add-new',
                    handler : function () {
                        var dept ={"name":$('#name').val(),"description":$('#description').val(),"parentId":$('#parentId').val()};
                        alert(dept);
                        if ($('#deptDialog').form('validate')) {
                            $.ajax({
                                url : '/depts',
                                type : 'POST',
                                contentType : 'application/json',
                                dataType : 'json',
                                data:JSON.stringify(dept),
                                beforeSend : function () {
                                    $.messager.progress({
                                        text : '正在'+'中......',
                                    });
                                },
                                success : function (data, response, status) {
                                    $.messager.progress('close');
                                    if (data != null || data > 0) {
                                        $.messager.show({
                                            title : '提示',
                                            msg : '功能成功！',
                                        });
                                        $('#deptDialog').dialog('close');
                                        $('#deptDateGrid').datagrid('reload');
                                    } else {
                                        $.messager.alert('失败！','未知错误导致失败，请重试！','warning');
                                    }
                                }
                            });
                        }

                    },
                },{
                    text : '取消',
                    iconCls : 'icon-redo',
                    handler : function (){
                        $('#deptDialog').dialog('close').form('reset');
                    },
                }],
            });
            $('#addDept').click(function(){
                $('#deptDialog').dialog('open');
                $('#deptDialog').dialog('refresh','/dept/add.ftl');
            });
            $('#deleteDept').click(function(){
                var rows = $('#deptDataGrid').datagrid('getSelections');
                if(rows.length > 0){
                    $.messager.confirm('确定操作','您正的要删除所选的记录吗？',function(flag){
                        if (flag){
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }

                            $.ajax({
                                type : 'delete',
                                url : '/depts/'+rows[0].id,
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#deptDataGrid').datagrid('loading');
                                },
                                success : function (data) {
                                    if (data) {
                                        $('#deptDataGrid').datagrid('loaded');
                                        $('#deptDataGrid').datagrid('load');
                                        $('#deptDataGrid').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : data + '个员工被删除成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('警告','请选择要删除的记录！','info');
                }
            });
            $('#updateDept').click(function(){
                $('#deptDialog').dialog('open');
                $('#deptDialog').dialog('refresh','/dept/update.ftl');
            });
        });
    </script>
</html>